<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_DM.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_DM,$login['staff_auth']);
$smarty->assign('now_menu', $aokimenu[AOKI_AUTH_COST_DM]["li"]);

$types = Class_M_OTHER::get_dmtype_array();
$dosearch = FALSE ;
$dao = new Class_M_COST_DM;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_FLIER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

$wherearr =  array();
$shop_name = NULL;

/*
if ( isset( $forms['shop_name'] ) &&  ( !empty( $forms['shop_name'] ) ) ){
	//$wherearr[] = sprintf(" shop_name like  '%s' ",  "%".$forms['shop_name']."%" );
	$shop_name = $forms['shop_name'];
}欺骗你。

*/

$martrix_cd = "";
if ( isset( $forms['week_cd'] ) &&  ( !empty( $forms['week_cd'] ) ) ){
	$wherearr[] = sprintf("matrix_cd =  '%s' ",  $forms['week_cd'] );
	$martrix_cd   =  $forms['week_cd'];
	
	$wdata = Class_M_OTHER::get_by_matrix_cd($martrix_cd);
}



//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "dmlist".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	
	$colums = array();
	$col_heads = array(
		'matrix_cd'      => array( 'マトリックスID' ,  1) ,
		'year4'          => array( '年度' ,  1) ,
		'week_cd'        => array( '週' ,  1) ,
		'flier_date'     => array( 'マトリックス折込日' ,  1),
		'week_date_begin'=> array( 'マトリックス開始日' ,  1),
		'week_date_end'  => array( 'マトリックス終了日' ,  1),
		'begin_date'     => array( 'セール期間開始日' ,  1),
		'end_date'       => array( 'セール期間終了日' ,  1),
		'dm_date'        => array( 'DM投函日' ,  1),
		'dm_no'          => array( '差替NO' ,  1),
		'shop_cd'        => array( '店舗コード' ,  1),
		'shop_name'      => array( '店舗名' ,  1),
		'count'          => array( '部数' ,  1),
		'title'          => array( 'タイトル' ,  1),
		'sort'           => array( '種類コード' ,  1),
		'sortnm'         => array( '種類名' ,      1),
		'haga_out'       => array( 'ハガキ 表' ,   1),
		'haga_in'        => array( 'ハガキ 裏' ,   1),
		'futou'          => array( '封筒' ,        1),
		'preken_out'     => array( 'プレ券 表' ,   1),
		'preken_in'      => array( 'プレ券 裏' ,   1),
		'wariken_out'    => array( '割引券 表' ,   1),
		'wariken_in'     => array( '割引券 裏' ,   1),
		'atemei_out'     => array( '宛名 表' ,     1),
		'atemei_in'      => array( '宛名 裏' ,     1),
		'pera1_out'      => array( 'ペラ① 表' ,  1),
		'pera1_in'       => array( 'ペラ① 裏' ,  1),
		'pera2_out'      => array( 'ペラ② 表' ,  1),
		'pera2_in'       => array( 'ペラ② 裏' ,  1),
		'ayisatu_out'    => array( '挨拶状 表' ,  1),
		'ayisatu_in'     => array( '挨拶状 裏' ,  1),
		'honjyou_out'    => array( '本状 表' ,  1),
		'honjyou_in'     => array( '本状 裏' ,  1),
		'other1'         => array( 'その他1' ,  1),
		'other2'         => array( 'その他2' ,  1),
		'other3'         => array( 'その他3' ,  1),
		'other4'         => array( 'その他4' ,  1),
		'other5'         => array( 'その他5',  1)
	);
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	
	$data = $dao->searchcsv( $wherearr, $page,$martrix_cd  );
	$wdata = Class_M_OTHER::get_by_matrix_cd($martrix_cd);
	
	$year4            = $wdata['year4' ];
	$week_cd          = $wdata['week_cd' ];
	$flier_date       = Class_PWD::int8_to_date( $wdata['flier_date' ]);
	$week_date_begin  = Class_PWD::int8_to_date( $wdata['week_date_begin' ]);
	$week_date_end    = Class_PWD::int8_to_date( $wdata['week_date_end' ]);
	
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["year4"]           =  $year4 ;
		$data[$k]["week_cd"]         =  $week_cd ;
		$data[$k]["flier_date"]      =  $flier_date ;
		$data[$k]["week_date_begin"] =  $week_date_begin ;
		$data[$k]["week_date_end"]   =  $week_date_end ;
		
		$data[$k]["sortnm"]      = $types[$v["sort"]];
		$data[$k]["begin_date"]  = Class_PWD::int8_to_date($v["begin_date"]);
		$data[$k]["end_date"]    = Class_PWD::int8_to_date($v["end_date"]);
		$data[$k]["dm_date"]     = Class_PWD::int8_to_date($v["dm_date"]);	
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了
	if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
		$dosearch = TRUE;
	}
	
	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];
	}else{
		$page['current'] = 1;
	}

	$page['item'] = PAGE_MAX;
	if ($dosearch) {
		//$page内容は関数内部更新されます。
		$data = $dao->search( $wherearr, $page,$martrix_cd  );
		$wdata = Class_M_OTHER::get_by_matrix_cd($martrix_cd);
		$weekname ="第".$wdata["week_cd"]."週";
		if (!empty($wdata["week_seq"])){
			$weekname = $weekname.$wdata["week_seq"]."回";
		}
		
		$weekBE = Class_PWD::int8_to_mmdd($wdata["week_date_begin"])."～".Class_PWD::int8_to_mmdd($wdata["week_date_end"]);
		//表示ために整形開始
		foreach ($data as $k => $v ) {
			$data[$k]["begin_date"] = Class_PWD::int8_to_date($v["begin_date"]);
			$data[$k]["end_date"]   = Class_PWD::int8_to_date($v["end_date"]);
			$data[$k]["dm_date"]    = Class_PWD::int8_to_date($v["dm_date"]);
			$data[$k]["weeknm"]     = $weekname;
			$data[$k]["weekbe"]     = $weekBE;
		}
		
	}

//表示ために整形完了
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );

$smarty->assign('dm_sort', $types );
if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_dm_index.html');
?>